#!/bin/bash

#example usage: ./command 1 eng1 127.0.0.1 8080 9090 127.0.0.1 8000 12345

echo "USAGE: $0 'ENGINE_ID' 'ENGINE_NAME' 'ENGINE_HOST' 'ENGINE_PORT' 'ALIVE_PORT' 'REPO_HOST' 'REPO_PORT' 'CONTROL_PORT'"
#                   $1           $2             $3           $4           $5            $6         $7           $8
if [ ! $1 ] ; then
	echo "Missing ENGINE_ID"
	exit 1
fi
ENGINE_ID=$1

if [ ! $2 ] ; then
	echo "Missing ENGINE_NAME"
	exit 1
fi
ENGINE_NAME=$2

if [ ! $3 ] ; then
	echo "Missing ENGINE_HOST"
	exit 1
fi
ENGINE_HOST=$3

if [ ! $4 ] ; then
	echo "Missing ENGINE_PORT"
	exit 1
fi
ENGINE_PORT=$4

if [ ! $5 ] ; then
	echo "Missing ALIVE_PORT"
	exit 1
fi
ALIVE_PORT=$5

if [ ! $6 ] ; then
	echo "Missing REPO_HOST"
	exit 1
fi
REPO_HOST=$6

if [ ! $7 ] ; then
	echo "Missing REPO_PORT"
	exit 1
fi
REPO_PORT=$7

if [ ! $8 ] ; then
	echo "Missing CTL_PORT"
	exit 1
fi
CTL_PORT=$8

BASE=/home/eros/Scrivania/POLIMI/SeCo-P2P/seco/seco-qp/trunk/seco-qp-server
MAVEN=/home/eros/.m2/repository
export SECO_CONF=/tmp/SECO_ENG_$ENGINE_PORT

echo $SECO_CONF

cd $BASE/src/main/assemblies/seco-qp-server/files

cp $BASE/src/main/resources/org/seco/qp/server/default.properties $SECO_CONF

sed -i "s/api\.port = [0-9][0-9]*/api.port = $ENGINE_PORT/" $BASE/src/main/resources/org/seco/qp/server/default.properties
sed -i "s/api\.port = [0-9][0-9]*/api.port = $ENGINE_PORT/" $BASE/target/classes/org/seco/qp/server/default.properties
sed -i "s/api\.port = [0-9][0-9]*/api.port = $ENGINE_PORT/" $SECO_CONF

sed -i "s/server\.name = [0-9a-zA-Z\-]*/server.name = $ENGINE_NAME/" $BASE/src/main/resources/org/seco/qp/server/default.properties
sed -i "s/server\.name = [0-9a-zA-Z\-]*/server.name = $ENGINE_NAME/" $BASE/target/classes/org/seco/qp/server/default.properties
sed -i "s/server\.name = [0-9a-zA-Z\-]*/server.name = $ENGINE_NAME/" $SECO_CONF

sed -i "/api\.host = [^\n]*/api.host = $ENGINE_HOST" $SECO_CONF
echo "api.host = $ENGINE_HOST" >> $SECO_CONF
sed -i "/api\.alive_port = [^\n]*/api.alive_port = $ALIVE_PORT" $SECO_CONF
echo "api.alive_port = $ALIVE_PORT" >> $SECO_CONF
sed -i "/routing\.repo_host = [^\n]*/routing.repo_host = $REPO_HOST" $SECO_CONF
echo "routing.repo_host = $REPO_HOST" >> $SECO_CONF
sed -i "/routing\.repo_PORT = [^\n]*/routing.repo_port = $REPO_PORT" $SECO_CONF
echo "routing.repo_port = $REPO_PORT" >> $SECO_CONF
sed -i "/routing\.engine_id = [^\n]*/routing.engine_id = $ENGINE_ID" $SECO_CONF
echo "ENGINE_ID - $ENGINE_ID"
echo "routing.engine_id = $ENGINE_ID" >> $SECO_CONF

export CLASSPATH=$BASE/target/classes:$MAVEN/org/seco/common/seco-common-utils/0.1.0-SNAPSHOT/seco-common-utils-0.1.0-SNAPSHOT.jar:$MAVEN/org/seco/common/seco-common-annotations/0.1.0-SNAPSHOT/seco-common-annotations-0.1.0-SNAPSHOT.jar:$MAVEN/com/google/guava/guava/10.0.1/guava-10.0.1.jar:$MAVEN/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:$MAVEN/ch/qos/logback/logback-classic/0.9.28/logback-classic-0.9.28.jar:$MAVEN/ch/qos/logback/logback-core/0.9.28/logback-core-0.9.28.jar:$MAVEN/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar:$MAVEN/org/seco/common/seco-common-validation/0.1.0-SNAPSHOT/seco-common-validation-0.1.0-SNAPSHOT.jar:$MAVEN/net/sf/oval/oval/1.40/oval-1.40.jar:$MAVEN/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:$MAVEN/org/seco/common/seco-common-reflection/0.1.0-SNAPSHOT/seco-common-reflection-0.1.0-SNAPSHOT.jar:$MAVEN/org/reflections/reflections/0.9.6/reflections-0.9.6.jar:$MAVEN/javassist/javassist/3.12.1.GA/javassist-3.12.1.GA.jar:$MAVEN/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:$MAVEN/com/googlecode/gentyref/gentyref/1.1.0-SNAPSHOT/gentyref-1.1.0-SNAPSHOT.jar:$MAVEN/org/seco/common/seco-common-repositories/0.1.0-SNAPSHOT/seco-common-repositories-0.1.0-SNAPSHOT.jar:$MAVEN/org/seco/common/seco-common-data/0.1.0-SNAPSHOT/seco-common-data-0.1.0-SNAPSHOT.jar:$MAVEN/org/seco/common/seco-common-serialization/0.1.0-SNAPSHOT/seco-common-serialization-0.1.0-SNAPSHOT.jar:$MAVEN/org/codehaus/jackson/jackson-core-asl/1.7.4/jackson-core-asl-1.7.4.jar:$MAVEN/org/seco/common/seco-common-cache/0.1.0-SNAPSHOT/seco-common-cache-0.1.0-SNAPSHOT.jar:$MAVEN/net/sf/ehcache/ehcache-core/2.4.0/ehcache-core-2.4.0.jar:$MAVEN/spy/memcached/2.4.2/memcached-2.4.2.jar:$MAVEN/org/seco/common/seco-common-web/0.1.0-SNAPSHOT/seco-common-web-0.1.0-SNAPSHOT.jar:$MAVEN/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar:$MAVEN/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar:$MAVEN/com/sun/jersey/jersey-server/1.2/jersey-server-1.2.jar:$MAVEN/com/sun/jersey/jersey-core/1.2/jersey-core-1.2.jar:$MAVEN/asm/asm/3.1/asm-3.1.jar:$MAVEN/org/eclipse/jetty/jetty-client/7.3.1.v20110307/jetty-client-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-http/7.3.1.v20110307/jetty-http-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-io/7.3.1.v20110307/jetty-io-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-util/7.3.1.v20110307/jetty-util-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-server/7.3.1.v20110307/jetty-server-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-continuation/7.3.1.v20110307/jetty-continuation-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-servlet/7.3.1.v20110307/jetty-servlet-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-security/7.3.1.v20110307/jetty-security-7.3.1.v20110307.jar:$MAVEN/org/apache/hadoop/hadoop-hbase/0.20.3/hadoop-hbase-0.20.3.jar:$MAVEN/org/apache/hadoop/hadoop-core/0.20.1/hadoop-core-0.20.1.jar:$MAVEN/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar:$MAVEN/org/apache/hadoop/hadoop-zookeeper/3.2.2/hadoop-zookeeper-3.2.2.jar:$MAVEN/voldemort/voldemort/0.80.1/voldemort-0.80.1.jar:$MAVEN/com/sun/jersey/jersey-client/1.2/jersey-client-1.2.jar:$MAVEN/org/seco/qp/seco-qp-api/1.1.0-SNAPSHOT/seco-qp-api-1.1.0-SNAPSHOT.jar:$MAVEN/org/seco/qp/seco-qp-common/1.1.0-SNAPSHOT/seco-qp-common-1.1.0-SNAPSHOT.jar:$MAVEN/org/eclipse/jetty/jetty-webapp/7.3.1.v20110307/jetty-webapp-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-xml/7.3.1.v20110307/jetty-xml-7.3.1.v20110307.jar:$MAVEN/org/eclipse/jetty/jetty-servlets/7.3.1.v20110307/jetty-servlets-7.3.1.v20110307.jar:$MAVEN/org/codehaus/jackson/jackson-mapper-asl/1.7.4/jackson-mapper-asl-1.7.4.jar:$MAVEN/org/mvel/mvel2/2.0.15/mvel2-2.0.15.jar:$MAVEN/org/seco/qp/seco-qp-engine/1.1.0-SNAPSHOT/seco-qp-engine-1.1.0-SNAPSHOT.jar:$MAVEN/commons-dbcp/commons-dbcp/1.4/commons-dbcp-1.4.jar:$MAVEN/commons-pool/commons-pool/1.5.4/commons-pool-1.5.4.jar:$MAVEN/org/apache/activemq/activemq-core/5.5.0/activemq-core-5.5.0.jar:$MAVEN/org/apache/geronimo/specs/geronimo-jms_1.1_spec/1.1.1/geronimo-jms_1.1_spec-1.1.1.jar:$MAVEN/org/apache/activemq/activeio-core/3.1.2/activeio-core-3.1.2.jar:$MAVEN/org/apache/geronimo/specs/geronimo-j2ee-management_1.1_spec/1.0.1/geronimo-j2ee-management_1.1_spec-1.0.1.jar:$MAVEN/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:$MAVEN/org/apache/activemq/kahadb/5.5.0/kahadb-5.5.0.jar:$MAVEN/org/apache/activemq/protobuf/activemq-protobuf/1.1/activemq-protobuf-1.1.jar:$MAVEN/org/osgi/org.osgi.core/4.1.0/org.osgi.core-4.1.0.jar:$MAVEN/org/springframework/spring-context/3.0.2.RELEASE/spring-context-3.0.2.RELEASE.jar:$MAVEN/org/springframework/spring-aop/3.0.2.RELEASE/spring-aop-3.0.2.RELEASE.jar:$MAVEN/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:$MAVEN/org/springframework/spring-asm/3.0.2.RELEASE/spring-asm-3.0.2.RELEASE.jar:$MAVEN/org/springframework/spring-beans/3.0.2.RELEASE/spring-beans-3.0.2.RELEASE.jar:$MAVEN/org/springframework/spring-core/3.0.2.RELEASE/spring-core-3.0.2.RELEASE.jar:$MAVEN/org/springframework/spring-expression/3.0.2.RELEASE/spring-expression-3.0.2.RELEASE.jar:$MAVEN/commons-net/commons-net/2.0/commons-net-2.0.jar:$MAVEN/org/jasypt/jasypt/1.7/jasypt-1.7.jar:$MAVEN/org/apache/camel/camel-jms/2.7.1/camel-jms-2.7.1.jar:$MAVEN/org/apache/camel/camel-core/2.7.1/camel-core-2.7.1.jar:$MAVEN/org/fusesource/commonman/commons-management/1.0/commons-management-1.0.jar:$MAVEN/org/apache/camel/camel-spring/2.7.1/camel-spring-2.7.1.jar:$MAVEN/org/springframework/spring-tx/3.0.5.RELEASE/spring-tx-3.0.5.RELEASE.jar:$MAVEN/org/springframework/spring-jms/3.0.5.RELEASE/spring-jms-3.0.5.RELEASE.jar:$MAVEN/com/kenai/nbpwr/org-slf4j-jdk14/1.5.11-201003102038/org-slf4j-jdk14-1.5.11-201003102038.jar:$MAVEN/org/slf4j/slf4j-jdk14/1.5.11/slf4j-jdk14-1.5.11.jar:$MAVEN/org/xerial/sqlite-jdbc/3.7.2/sqlite-jdbc-3.7.2.jar:$MAVEN/org/springframework/spring-context-support/3.0.2.RELEASE/spring-context-support-3.0.2.RELEASE.jar:$MAVEN/org/jdom/jdom/1.1/jdom-1.1.jar:$MAVEN/org/slf4j/log4j-over-slf4j/1.6.1/log4j-over-slf4j-1.6.1.jar:$MAVEN/org/slf4j/jul-to-slf4j/1.6.1/jul-to-slf4j-1.6.1.jar:$MAVEN/com/google/protobuf/protobuf-java/2.3.0/protobuf-java-2.3.0.jar:$MAVEN/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar:$MAVEN/postgresql/postgresql/9.0-801.jdbc4/postgresql-9.0-801.jdbc4.jar

JARFILE=$MAVEN/org/seco/qp/seco-qp-engine/1.1.0-SNAPSHOT/seco-qp-engine-1.1.0-SNAPSHOT.jar
FILES=`find $BASE/../seco-qp-engine/src/main/java/org/seco/qp/engine/ -type f -newer $JARFILE -name "*.java"`
#FILES="$BASE/../seco-qp-engine/src/main/java/org/seco/qp/engine/invokers/CompositeInvoker.java $BASE/../seco-qp-engine/src/main/java/org/seco/qp/engine/invokers/RemoteInvoker.java $FILES"
if [ -n "$FILES" ] ; then
	echo COMPILING
	echo $FILES | xargs javac -Xlint -d $BASE/../seco-qp-engine/target/classes/ &> /dev/stdout | grep -i -C4 error
	jar cvf $JARFILE -C $BASE/../seco-qp-engine/target/classes/ org | grep -i -C4 error
fi
echo RUNNING
java org.seco.qp.server.Server start --control-port $CTL_PORT
